package com.example.myfirstapplication.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.myfirstapplication.R
import com.example.myfirstapplication.utils.Tag
import com.example.myfirstapplication.data.News
import com.example.myfirstapplication.utils.myToast

class NewsRecyclerListAdapter(private val cxt: Context, private val dataList: ArrayList<News>) :
    RecyclerView.Adapter<NewsRecyclerListAdapter.NewsListViewHolder>() {
    class NewsListViewHolder(viewItem: View) : RecyclerView.ViewHolder(viewItem) {
        var newsTitle: TextView = viewItem.findViewById(R.id.news_Title)
        var newsThumb: ImageView = viewItem.findViewById(R.id.news_Thumb)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsListViewHolder {
        return NewsListViewHolder(
            LayoutInflater.from(cxt).inflate(R.layout.newslistitem2_layout, null, false)
        )
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
    override fun onBindViewHolder(holder: NewsListViewHolder, position: Int) {
        holder.newsTitle.text = dataList[position].title
        Glide.with(cxt).load(dataList[position].thumb).into(holder.newsThumb)
        holder.newsTitle.setOnClickListener{
            Log.i(Tag,"点击了${dataList[position]}")
            myToast(
                cxt,
                "你点击的是${dataList[position]}"
            )
        }
    }
}
